home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_brushedmetal3.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  2.2 KB  |  65 lines

  1. /* Renamed to LGbrushedmetal for RMR -- tal@SpamSucks_cs.caltech.edu */
  2.  
  3. /*
  4.  * Greg Ward Larson's anisotropic specular local illumination model.
  5.  * The derivation and formulae can be found in:  Ward, Gregory J.
  6.  * "Measuring and Modeling Anisotropic Reflection," ACM Computer
  7.  * Graphics 26(2) (Proceedings of Siggraph '92), pp. 265-272, July, 1992.
  8.  * Inputs:
  9.  *   N - unit surface normal
  10.  *   V - unit viewing direction (from P toward the camera)
  11.  *   xdir - a unit tangent of the surface which defines the reference
  12.  *          direction for the anisotropy.
  13.  *   xroughness - the apparent roughness of the surface in xdir.
  14.  *   yroughness - the roughness for the direction of the surface
  15.  *          tangent which is perpendicular to xdir.
  16.  */
  17. color
  18. LocIllumWardAnisotropic (normal N;  vector V;
  19.                          vector xdir;  float xroughness, yroughness;)
  20. {
  21.     float sqr (float x) { return x*x; }
  22.  
  23.     float cos_theta_r = clamp (N.V, 0.0001, 1);
  24.     vector X = xdir / xroughness;
  25.     vector Y = (N ^ xdir) / yroughness;
  26.  
  27.     color C = 0;
  28.     extern point P;
  29.     illuminance (P, N, PI/2) {
  30.         /* Must declare because extern L & Cl because we're in a function */
  31.         extern vector L;  extern color Cl;
  32.         float nonspec = 0;
  33.         lightsource ("__nonspecular", nonspec);
  34.         if (nonspec < 1) {
  35.             vector LN = normalize (L);
  36.             float cos_theta_i = LN . N;
  37.             if (cos_theta_i > 0.0) {
  38.                 vector H = normalize (V + LN);
  39.                 float rho = exp (-2 * (sqr(X.H) + sqr(Y.H)) / (1 + H.N))
  40.                     / sqrt (cos_theta_i * cos_theta_r);
  41.                 C += Cl * ((1-nonspec) * cos_theta_i * rho);
  42.             }
  43.         }
  44.     }
  45.     return C / (4 * xroughness * yroughness);
  46. }
  47.  
  48.  
  49.  
  50. surface
  51. k3d_brushedmetal3 ( float Ka = 1, Kd = 0.1, Ks = .9;
  52.         float uroughness = 0.35, vroughness = 0.2; 
  53.         color basecolor = color (.5, .5, .5);
  54. )
  55. {
  56.     normal Nf = faceforward (normalize(N), I);
  57.     vector xdir = normalize (dPdu);
  58.  
  59.     color spec = LocIllumWardAnisotropic (Nf, -normalize(I),
  60.                                           xdir, uroughness, vroughness);
  61.     Ci = basecolor * (Ka*ambient() + Kd*diffuse(Nf) + Ks*spec);
  62.     Oi = Os;  Ci *= Oi;
  63. }
  64.  
  65.